package com.ad.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ad.hibernate.User;

import com.ad.system.Common;

public class UserDao extends HibernateDaoSupport implements IUserDao {

	@Override
	public String insert(User user) {
		user.setPass(Common.MD5(user.getPass()));
		return (String)this.getHibernateTemplate().save(user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> Check(String name, String password) {
		List<User> result = this.getHibernateTemplate().find(
				"from User where name ='" + name + "'" + " and password ='"
						+ password + "'");
		if (result == null || result.size() == 0) {
			return null;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	// 小弟的测试用户名是否重名
	@Override
	public boolean Check(String name) {
		List<User> result = this.getHibernateTemplate().find(
				"from User where name ='" + name + "'");
		if (result == null || result.size() == 0) {
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public User findByEmail(String email) {
		List<User> result = this.getHibernateTemplate().find(
				"from User where name='" + email + "'");
		if (result == null || result.size() != 1) {
			return null;
		}
		return result.get(0);
	}
	

	@Override
	public void update(User user) {
		this.getHibernateTemplate().update(user);
	}
}
